/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.stripes.webservice;

import com.google.inject.Inject;
import com.oas.thit._this.balenq.xsd.BalanceEnquiryRequestBody;
import com.oas.thit._this.xsd.THISBaseDataElementBody;
import com.oas.thit.xmlmessage.xsd.Header;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.JAXBElement;
import th.manager.SequenceManager;
import th.model.SeqNum;

/**
 *
 * @author fikri
 */
public class WebServiceUtil {

    @Inject
    private SequenceManager seqManager;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    
    private static String PROFILE_ENQ_MSG_ID = "TPDPPROFWD01";
    
    private static String HEADER_ID_PROFILE_ENQUIRY = "THSDPQR";

    public String getHeaderID(String msgID, String headerID) {
        String date = sdf.format(new Date());
        String seq = seqManager.getSequence(msgID);
        SeqNum seqNum = new SeqNum();
        seqNum.setNum(Integer.parseInt(seq));
        seqNum.setUrusan(msgID);
        seqManager.updateSequece(seqNum);
        return headerID + date + seq;
    }

    public JAXBElement<Header> createHeader( String msgID, String headerID ) {
        
        com.oas.thit.xmlmessage.xsd.ObjectFactory objHeader = new com.oas.thit.xmlmessage.xsd.ObjectFactory();
        Header reqHead = objHeader.createHeader();
        reqHead.setMsgId(objHeader.createHeaderMsgId(msgID));
        reqHead.setResponseCode(objHeader.createHeaderResponseCode("0000"));
        String txId = getHeaderID(msgID, headerID);
        reqHead.setTxid(objHeader.createHeaderTxid(txId));
        return objHeader.createEventHeader(reqHead);
    }
    
    public String checkAkaunWithKp(String akaun, String kp) {

        com.oas.thit._this.services.THISServices service = new com.oas.thit._this.services.THISServices();
            com.oas.thit._this.services.THISServicesPortType port = service.getTHISServicesHttpSoap11Endpoint();
            // TODO initialize WS operation arguments here
            com.oas.thit._this.xsd.THISRequest req = new com.oas.thit._this.xsd.THISRequest();
            com.oas.thit._this.xsd.ObjectFactory objFactory = new com.oas.thit._this.xsd.ObjectFactory();
            
            req.setHeader( createHeader(PROFILE_ENQ_MSG_ID, HEADER_ID_PROFILE_ENQUIRY) );
            
            THISBaseDataElementBody reqBody = objFactory.createTHISBaseDataElementBody();
            reqBody.setTHAcct(objFactory.createTHISBaseDataElementBodyTHAcct(akaun));
            reqBody.setTHICNo(objFactory.createTHISBaseDataElementBodyTHICNo(kp));
            
            JAXBElement<THISBaseDataElementBody> jReqBody =  objFactory.createTHISRequestBody(reqBody);
            req.setBody(jReqBody);
            
            // TODO process result here
            com.oas.thit._this.profile.depositor.xsd.DepositorProfileResponse result = port.depositorProfileQuery(req);
            Header header = result.getHeader().getValue();
            return header.getResponseCode().getValue();
        
    }
}
